//
//  HMWebViewController.m
//  HMSDK
//
//  Created by hm on 17/5/29.
//  Copyright © 2017年 hm. All rights reserved.
//

#import "HMWebViewController.h"
#import "HMAD.h"

//设备屏幕高度
#ifndef UIScreenHeight
#define UIScreenHeight   [UIScreen mainScreen].bounds.size.height
#endif
//设备屏幕宽度
#ifndef UIScreenWidth
#define UIScreenWidth    [UIScreen mainScreen].bounds.size.width
#endif
//声明弱引用对象，常在blocks中使用
#define WEAKSELF typeof(self) __weak weakSelf = self;

@interface HMWebViewController ()<UIWebViewDelegate>
@property(nonatomic, strong) UIWebView *mainWebView;
@property(nonatomic, copy) NSString *URL;

@property(nonatomic, strong) NSString *barTitle;
@property(nonatomic, copy) UIColor *barTitleColor;
@property(nonatomic, copy) UIColor *bgColor;
@property(nonatomic, retain) UIImage *backImage;
@property(nonatomic, strong) NSString *backText;
@property(nonatomic, assign) BOOL animated;

@end

@implementation HMWebViewController

+ (instancetype)createWebWithURL:(NSString *)url
{
    HMWebViewController *web = [[HMWebViewController alloc] init];
    web.URL = url;
    return web;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.mainWebView.frame = CGRectMake(0, 0, UIScreenWidth, UIScreenHeight);
    
    [self.view addSubview:self.mainWebView];
    
    NSString *encodedString=[self.URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //请求
    NSURL *url =  [NSURL URLWithString:encodedString];
    
    //每次打开webview都是实时刷新
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
    
    [self.mainWebView loadRequest:request];
    
    if(_backImage==nil){
        NSString *bt;
        if (self.backText !=nil){
            bt = self.backText;
        } else {
            bt = @"返回";
        }
        
        UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:bt style:UIBarButtonItemStylePlain target:self action:@selector(clickBack)];
        self.navigationItem.leftBarButtonItem = left;
    } else {
        UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:_backImage style:UIBarButtonItemStylePlain target:self action:@selector(clickBack)];
        self.navigationItem.leftBarButtonItem = left;
    }

    if (_barTitle != nil) {
        self.navigationItem.title = _barTitle;
    }
    //set NavigationBar 背景颜色&title 颜色
    if(_bgColor!=nil){
        [self.navigationController.navigationBar setBarTintColor:_bgColor];
    }
    if(_barTitleColor!=nil){
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:_barTitleColor,NSForegroundColorAttributeName,nil]];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.navigationItem.title  = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"error:%@",error.description);
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:self.animated];
    if ([self.delegate respondsToSelector:@selector(clickDismissWebVC)]) {
        [self.delegate clickDismissWebVC];
    }
}

- (void)clickBack {
    [self dismissViewControllerAnimated:self.animated completion:^{
       
    }];
}

- (UIWebView *)mainWebView
{
    if (!_mainWebView) {
        _mainWebView = [[UIWebView alloc] init];
        _mainWebView.opaque = NO;
        _mainWebView.delegate = self;
        _mainWebView.backgroundColor = [UIColor whiteColor];

    }
    return _mainWebView;
}

@end
